package com.powerManager.sdk.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.RandomAccess;

import com.powerManager.sdk.entity.User;

/** -- 权限控制类
 * @author lqw
 *
 */
public class Security implements ISecurityUser{

	private User user = null;
	
	private List<Map<String, Object>> roles = null;
	
	private Map<String, Object> dept = null;
	
	//private List<Map<String, Object>> permit = null;
	
	/** -- 判断用户是否有访问该路径的权限
	 * @param user
	 * @param moduleId
	 * @return 通过返回:true,否则返回:false
	 */
	public boolean isAdopt(String moduleId){
		
		return true;
	}
	
	public List<Map<String, Object>> showMenu(){
		//if(permit != null) return permit;
		List<Map<String, Object>> permit = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> forbid = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> privilege = new ArrayList<Map<String,Object>>();
		
		for (Map<String, Object> map : InitSystem.privilege) {
			String privilegeMaster = String.valueOf(map.get("PrivilegeMaster"));
			if(privilegeMaster!=null && privilegeMaster.equals("CF_Role")){
				String key = String.valueOf(map.get("privilegeMasterKey"));
				for(Map<String, Object> r : roles){
					String roleId = String.valueOf(r.get("role_id"));
					if(roleId != null && roleId.equals(key)){
						privilege.add(map);
					}
				}
			}
			if(privilegeMaster!=null && privilegeMaster.equals("CF_User")){
				String key = String.valueOf(map.get("privilegeMasterKey"));
				if(key != null && key.equals(user.getUser_id())){
					privilege.add(map);
				}
			}
		}
	
		
		for (int i=0;i<InitSystem.menu.size();i++) {
			Map<String, Object> map = InitSystem.menu.get(i);
			String menuId = String.valueOf(map.get("MenuID"));
			for (Map<String, Object> p : privilege) {
				String privilegeAccess = String.valueOf(p.get("PrivilegeAccess"));
				if(privilegeAccess != null && privilegeAccess.equals("Sys_Menu")){
					String key = String.valueOf(p.get("PrivilegeAccessKey"));
					String op = String.valueOf(p.get("PrivilegeOperation"));
					if(key != null && key.equals(menuId)){
						if(op!=null && op.equals("Permit")){
							permit.add(map);
						}else{
							forbid.add(map);
						}
					}
				}
			}
		}
		
		for (Map<String,Object> map : forbid) {
			permit.remove(map);
		}
		
		List<Map<String, Object>> permit1 = 
			new  ArrayList<Map<String, Object>>();
		
		//Collections.addAll(permit1,  (List<Map<String, Object>>)Arrays.asList(new Object[permit.size()]));
		permit1 = this.copyArrayList(permit);
		//Collections.copy(permit1, permit);
		PagingResult pr = new PagingResult();
		return pr.toTree(permit1, "MenuNo", "MenuParentNo", "0");
	}

	@Override
	public User currentUser() {
		return user;
	}

	@Override
	public List<Map<String, Object>> currentRole() {
		return roles;
	}

	@Override
	public Map<String, Object> currentDept() {
		return dept;
	}

	@Override
	public void setCurrentUser(User user) {
		this.user = user;
	}

	@Override
	public void setCurrentRole(List<Map<String, Object>> roles) {
		this.roles = roles;
	}

	@Override
	public void setCurrentDept(Map<String, Object> dept) {
		this.dept = dept;
	}

	@Override
	public List<Map<String, Object>> showButton(String menuId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<Map<String, Object>> copyArrayList(List<Map<String, Object>> src){
		int srcSize = src.size();
		List<Map<String, Object>> listDest =new  ArrayList<Map<String, Object>>();
        //if (srcSize > dest.size())
            //throw new IndexOutOfBoundsException("Source does not fit in dest");

    
	    //ListIterator<Map<String, Object>> si=src.listIterator();
            for (int i=0; i<srcSize; i++) {
            	Map<String, Object> hsSource= new HashMap<String, Object>() ;  
            	Map<String, Object> hsDist= new HashMap<String, Object>() ;  
            	hsSource=src.get(i);
            	Iterator iterator_1 = hsSource.keySet().iterator();
            	while (iterator_1.hasNext()) {
	            	String key =(String) iterator_1.next();
	            	//System.out.println("tab.get(key) is :"+tab.get(key));
	            	hsDist.put(key, hsSource.get(key));
            	} 
            	listDest.add(hsDist);
               // di.next();
                //di.set(si.next());
           }
        
            
        return listDest;
	}
	
	
	public static void main(String[] args) {
		List<String> arr1 = new ArrayList<String>();
		arr1.add("1");
		arr1.add("2");
		arr1.add("3");
		List<String> arr2 = new ArrayList<String>(Arrays.asList(new String[arr1.size()]));
		Collections.copy(arr2, arr1);
		arr2.add("4");
		System.out.println(arr2.size()+":"+arr1.size());
	}
}
